package com.vincent.template.commons.exception;

import com.vincent.template.commons.base.BaseFieldPart;
import org.apache.commons.collections.CollectionUtils;

import java.util.Collection;

/**
 * @Author Vincent Wang
 * @Date 2020/10/16 16:33
 */
public class Vct520Exception extends RuntimeException {
    private static final long serialVersionUID = 5080851408984628323L;

    private String message;
    private String code;
    private String messageKey;
    private Object[] args;

    public Vct520Exception(String message) {
        this.message = message;
    }

    public Vct520Exception(String messageKey, String message) {
        super(message);
        this.messageKey = messageKey;
        this.message = message;
    }

    public Vct520Exception(String messageKey, String message, Throwable throwable) {
        super(message, throwable);
        this.messageKey = messageKey;
        this.message = message;
    }

    public Vct520Exception(String messageKey, String message, Object[] args) {
        super(message);
        this.messageKey = messageKey;
        this.message = message;
        this.args = args;
    }

    public static void throwIfTrue(boolean result, String message) {
        if (result) {
            throw new Vct520Exception(message);
        }
    }

    public static void throwIfEmpty(Object obj, String message) {
        if (obj == null) {
            throw new Vct520Exception(message);
        }
        if (obj instanceof String) {
            if (((String) obj).isEmpty()) {
                throw new Vct520Exception(message);
            }
        } else if (obj instanceof Collection<?>) {
            if (CollectionUtils.isEmpty((Collection<?>) obj)) {
                throw new Vct520Exception(message);
            }
        } else if (obj instanceof BaseFieldPart) {
            if (((BaseFieldPart) obj).isDeleted()) {
                throw new Vct520Exception(message);
            }
        }
    }

    @Override
    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessageKey() {
        return this.messageKey;
    }

    public void setMessageKey(String messageKey) {
        this.messageKey = messageKey;
    }

    public Object[] getArgs() {
        return this.args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

}
